package ai.people.netmon.framework.domain.warning.vo;

import ai.people.netmon.framework.domain.warning.entity.WarningSubject;
import ai.people.netmon.utils.StringPool;
import ai.people.netmon.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 主题分析
 *
 * @author yuanqinglong
 * @date 2022/8/1 14:54
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SubjectAnalysisVO {


    /**
     * id
     */
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long id;

    /**
     * 主题名称
     *
     * @mock 中午吃什么
     */
    private String subjectName;

    /**
     * 开始时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime subjectStartTime;

    /**
     * 结束时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime subjectEndTime;

    /**
     * 主题平台
     *
     * @mock "telegram","twitter" 检索条件下拉
     */
    private String subjectPlatform;

    /**
     * 主题中文名称
     */
    private String subjectPlatformName;

    /**
     * 主题关键字
     */
    private String subjectKeyword;

    /**
     * 主体排除
     */
    private String subjectExclude;


    /**
     * 构建
     *
     * @param warningSubject 警告主题
     * @return {@link SubjectAnalysisVO}
     */
    public static SubjectAnalysisVO build(WarningSubject warningSubject, Map<String, String> platformMap) {
        SubjectAnalysisVO subjectAnalysisVO = JSON.parseObject(JSON.toJSONString(warningSubject), SubjectAnalysisVO.class);
        String platformNames = Stream.of(warningSubject.getSubjectPlatform().split(StringPool.COMMA)).map(platformMap::get).filter(StringUtils::isNotEmpty).collect(Collectors.joining(StringPool.COMMA));
        subjectAnalysisVO.setSubjectPlatformName(platformNames);
        return subjectAnalysisVO;
    }
}
